package com.threeCountry.threadtest;

/**
 * Created by yijy on 2017/12/29.
 * 死锁
 */
public class DeadLockDemo {
    public static void main(String[] args) {
        for(int i=0;i<300;i++){
            Thread t1 = new Thread(new LockThread(Yi.object1,Yi.object2));
            Thread t2 = new Thread(new LockThread(Yi.object2,Yi.object1));
            t1.setName("object1-"+i);
            t2.setName("object2-"+i);
            t1.start();
            t2.start();
        }
    }
}
class Yi{
    static String object1 = "object1";
    static Object object2 = "object2";
}
class LockThread implements Runnable{
    Object object1;
    Object object2;
    public LockThread(Object object1,Object object2) {
        this.object1 = object1;
        this.object2 = object2;
    }
    @Override
    public void run() {
            synchronized (object1){
                System.out.println(Thread.currentThread().getName()+" :"+object1+" lock****************");
                synchronized (object2){
                    System.out.println(Thread.currentThread().getName()+" :"+object2+" lock********************");
                }
            }
    }
}



